package first.niuke.simple;

/**
 * @ClassName NC22
 * @description:
 * @author: 荣燊
 * @create: 2021-04-08 17:33
 **/
public class NC22 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 5, 8};
        int[] b = {4, 6, 7, 9};
        merge(a, 5, b, 4);

        for (int i=0; i<a.length; i++) {
            System.out.println(a[i]);
        }
    }

    public static void merge(int A[], int m, int B[], int n) {
        //注意这道题说明了数组A有足够的数组空间，意思是A数组的大小不是m
        int i = m-1;
        int j = n-1;
        int index = m+n-1;
        while (i>=0 && j>=0) { //如果B先遍历完，不用担心，A就不用遍历了
            A[index--] = (A[i] >= B[j]) ? A[i--] : B[j--];
        }
        //如果A完了，B没完
        while (j>=0) {
            A[index--] = B[j--];
        }

    }
}
